<?php
namespace app\admin\controller;

use app\common\model\UsedClassify as UsedClassifyModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;

/**
 * 管理
 * Class Adminusedclassify
 * @package app\admin\controller
 *  默认(车型ID:1,品牌ID:2,车系ID:3,型号ID:4)
 */
class UsedClassify1 extends AdminBase
{
    protected $usedclassify_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->usedclassify_model = new UsedClassifyModel();
    }

    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $page = 1, $pid = 0)
    {
        $map = [];
        if ($keyword) {
            $map['pid'] = ['like', "%{$keyword}%"];
        }
        $usedclassify_list = $this->usedclassify_model->where($map)->where('pid',0)->order('sort,id DESC')->paginate(15, false, ['page' => $page]);

        return $this->fetch('index', ['usedclassify_list' => $usedclassify_list, 'keyword' => $keyword]);
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'UsedClassify1');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->usedclassify_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $usedclassify = $this->usedclassify_model->where('pid',0)->find($id);

        return $this->fetch('edit', ['usedclassify' => $usedclassify]);
    }

    /**
     * 更新
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'UsedClassify1');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->usedclassify_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除
     * @param $id
     */
    public function delete($id = '')
    {
        if($id){
                $fileImg = $this->usedclassify_model->where('id',$id)->value('thumb');
                if($fileImg){
                   if(file_exists('./'.$fileImg)){
                       unlink('./'.$fileImg);
                   } 
                }
        }
    
        if ($id) {
            $delete_cid = $this->usedclassify_model->where('id',$id)->value('cid');
            if($this->usedclassify_model->where('pid',$delete_cid)->delete()){
                if ($this->usedclassify_model->destroy($id)) {
                    $this->success('删除成功');
                } else {
                    $this->error('删除失败');
                }
            } else {
                $this->error('栏目下分类删除失败');
            }
        } else {
            $this->error('请选择需要删除的内容');
        }
    }

}